<?php

/**
 * @name IndexController
 * @desc 默认控制器
 */
class IndexController extends AdminController
{
    // 后台初始化
    public function indexAction()
    {
        if ($this->get('ajax')) {
            $result = [
                'homeInfo' => [
                    'title' => '首页',
                    'href' => '/admin/index/main'
                ],
                'logoInfo' => [
                    'title' => $this->config->application->name,
                    'image' => 'images/logo.png',
                    'href' => ''
                ],
                'menuInfo' => []
            ];
            // 加载菜单
            $allMenus = AdminMenusModel::getMenus();
            foreach ($allMenus as $k => $v) {
                if (isset($v['child']) && !empty($v['child'])) {
                    $allMenus[$k]['href'] = '';
                    foreach ($v['child'] as $k2 => $v2) {
                        if (!AdminUsersModel::check_right($v2['href'])) {
                            unset($allMenus[$k]['child'][$k2]);
                        }
                    }
                    if (isset($allMenus[$k]['child']) && empty($allMenus[$k]['child'])) unset($allMenus[$k]);
                } else {
                    // 无下级检测本身
                    if (!AdminUsersModel::check_right($v['href'])) {
                        unset($allMenus[$k]);
                    }
                }
            }
            $result['menuInfo'] = $allMenus;
            $this->result('ok', 0, $result);
        }else{
            $this->view->assign('user',$this->user['userinfo']);
        }
    }

    public function mainAction()
    {
        if (empty($this->user['userinfo']->google_secret)) {
            if ($this->ispost) {
                $post_data = [];
                // 验证绑定二维码
                $verifycode = $this->post('verifycode');
                $google2fa_key = $this->post('google2fa_key');
                if ($google2fa_key && $verifycode) {
                    // 验证
                    if (Google2FA::verify($google2fa_key, $verifycode)) {
                        // 成功
                        $post_data['google_secret'] = $google2fa_key;
                        // 更新
                        AdminUsersModel::update_all([
                            'conditions' => ['id = ?', $this->user['userinfo']->id],
                            'set' => $post_data
                        ]);
                        $this->ajaxresult(0, '绑定成功,请重新登录!');
                    } else {
                        // 验证失败
                        $this->ajaxresult(1, '验证码验证失败');
                    }
                } else {
                    // 验证码为空了
                    $this->ajaxresult(1, '验证码不能为空');
                }
            }
            $this->view->assign('bind_google_secret', true);
            $this->view->assign('data', $this->user['userinfo']);
            $key = Google2FA::get_secret_key();
            $qrcodeurl = Google2FA::get_qrcode_url($key, $this->user['userinfo']->username);
            $this->view->assign('qrcodeurl', $qrcodeurl);
            $this->view->assign('google2fa_key', $key);
        }
    }
}